package com.browsesoft.log;

import java.util.Hashtable;

import com.browsesoft.Entity;
import com.browsesoft.baseadapter.BasicEntity;
import com.browsesoft.tools.BSCalendar;
import com.browsesoft.user.User;

/**
 * 系统日志
 */
public class OperateLog extends BasicEntity {

	private static final long serialVersionUID = 6297339933455619013L;

	private static String tableName = "t_operatelog";

	public OperateLog() {
		super();
	}

	public OperateLog(Hashtable ht) {
		super(ht);
	}

	/**
	 * 日值信息插入操作
	 * 
	 * @param user
	 * @throws Exception
	 */
	private void recordLog(User user) throws Exception {
		// 放入基本属性
		this.putInBasicAttr(user);
		this.insertToDB();
	}

	/**
	 * 登陆日志
	 * 
	 * @param user
	 * @throws Exception
	 */
	public void loginLog(User user) throws Exception {
		// 操作
		this.attributes.put("operate", "登录");
		this.recordLog(user);
	}

	/**
	 * 退出系统日志
	 * 
	 * @param user
	 * @throws Exception
	 */
	public void exitLog(User user) throws Exception {
		this.attributes.put("operate", "退出");
		this.recordLog(user);
	}

	public void addLog(User user, Entity entity) throws Exception {
		record(user, entity, "增加");
	}

	/**
	 * 一般实体修改操作日志
	 * 
	 * @param user
	 * @param entity
	 * @throws Exception
	 */
	public void updateLog(User user, Entity entity) throws Exception {
		record(user, entity, "修改");
	}

	/**
	 * 一般实体删除操作日志
	 * 
	 * @param user
	 * @param entity
	 * @throws Exception
	 */
	public void deleteLog(User user, Entity entity) throws Exception {
		record(user, entity, "删除");
	}

	/**
	 * 自定义实体操作日志
	 * 
	 * @param user
	 * @param entity
	 * @param operateType
	 * @throws Exception
	 */
	private void record(User user, Entity entity, String operateType)
			throws Exception {
		// 实体类型，对象id号
		String entitytype = (String) entity.getAttributes().get("entitytype");
		String id = entity.getID();
		this.attributes.put("operate", operateType + "实体类型为" + entitytype
				+ ",ID号为" + id + "的对象.");
		this.recordLog(user);
	}

	/**
	 * 向集成方发送信息日值记录
	 */
	public void recordWebserviceMessage(User operateUser, String operateMessage) {
		try {
			this.attributes.put("operate", operateMessage);
			this.recordLog(operateUser);
		} catch (Exception e) {
			throw new RuntimeException("记录日值时发生错误!");
		}
	}

	/**
	 * 对实体的操作日值
	 */
	public void logEntityOperate(User user, String type, String operateDesc) {
		try {
			// 放入操作类型
			this.attributes.put("type", type);
			this.attributes.put("operate", operateDesc);
			this.recordLog(user);
		} catch (Exception e) {
			throw new RuntimeException("记录日志时发生错误!", e);
		}
	}

	/**
	 * 放入基本属性(表名，用户id,远程ip,登陆时间,当前时间,表名)
	 */
	private void putInBasicAttr(User user) {
		if (user != null) {
			this.attributes.put("userid", user.getID());
			this.attributes.put("username", user.getName());
			this.attributes.put("loginip", user.getAttributes().get("loginip"));
			this.attributes.put("logintime", user.getAttributes().get(
					"logintime"));
		}
		String time = BSCalendar.now().toString("yyyy-mm-dd hh:tt");
		this.attributes.put("time", time);
		this.attributes.put("tablename", tableName);
	}
}